package com.testtask.rightandabove.service;

import com.testtask.rightandabove.dao.ICDWarehouseDao;
import com.testtask.rightandabove.entity.CD;
import com.testtask.rightandabove.entity.Catalog;
import com.testtask.rightandabove.form.UploadForm;
import com.testtask.rightandabove.tools.Pager;
import com.testtask.rightandabove.tools.ServiceFunction;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * Service class
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
@Service
public class CDWarehouseServiceImpl implements ICDWarehouseService {

    @Autowired
    private ICDWarehouseDao cdWarehouseDao;

    /**
     * Searches for entities in the warehouse
     *
     * @param path
     * @param pageStr
     * @return List of CD
     */
    @Override
    public synchronized List<CD> findAll(String path, String pageStr) {
        List<CD> cdList = new ArrayList<CD>();
        try {
            File xmlFile = new File(path + "\\CDWarehouse.xml");
            cdList = cdWarehouseDao.readAll(xmlFile).getCds();
        } catch (JAXBException ex) {
            Logger.getLogger(CDWarehouseServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        List<CD> subList = new Pager().findAllPage(cdList, pageStr);
        return subList;
    }

    /**
     * Updates the file warehouse
     *
     * @param path
     * @param uploadForm
     */
    @Override
    public synchronized void upload(String path, UploadForm uploadForm) {
        MultipartFile file = uploadForm.getFileData();
        Catalog catalogInner = null;
        List<CD> cdListInner = new ArrayList<CD>();
        List<CD> cdListOuter = new ArrayList<CD>();
        List<CD> newCDListInner = new ArrayList<CD>();
        List<CD> newCDListOuter;
        if (file.getSize() > 0 && file.getSize() < 1048576) {
            try {
                File xmlFileInner = new File(path + "\\CDWarehouse.xml");
                File xmlFileOuter = ServiceFunction.multipartToFile(file);
                Catalog catalogOuter = cdWarehouseDao.readAll(xmlFileOuter);
                catalogInner = cdWarehouseDao.readAll(xmlFileInner);
                cdListInner = catalogInner.getCds();
                cdListOuter = catalogOuter.getCds();
            } catch (JAXBException ex) {
                Logger.getLogger(CDWarehouseServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalStateException ex) {
                Logger.getLogger(CDWarehouseServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(CDWarehouseServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            newCDListOuter = new ArrayList(cdListOuter);
            for (int i = 0; i < cdListInner.size(); i++) {
                newCDListInner.add(cdListInner.get(i));
                for (int j = 0; j < cdListOuter.size(); j++) {
                    if (cdListOuter.get(j).getTitle().equals(newCDListInner.get(newCDListInner.size() - 1).getTitle())) {
                        newCDListInner.set((newCDListInner.size() - 1), cdListOuter.get(j));
                        newCDListOuter.set(j, null);
                    }
                }
            }
            newCDListInner.addAll(newCDListOuter);
            new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator")
                    + file.getOriginalFilename()).delete();
            catalogInner.setCds(newCDListInner);
            try {
                cdWarehouseDao.updateAll(catalogInner, new File(path + "\\CDWarehouse.xml"));
            } catch (JAXBException ex) {
                Logger.getLogger(CDWarehouseServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
